import 'package:flutter/material.dart';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:example/utils/dialog_ext.dart';
import '../../base/custom_dialog.dart';
import '../../config/color_config.dart';
import '../../utils/common.dart';

/// 通用时间选择器
class DateTimeDialog extends CustomDialog<DateTimeDialogController> {
  DateTimeDialog({super.key,
    DateTimeDialogController? controller,
    String? title,
    int? type = PickerDateTimeType.kYMD,
    List<int>? customColumnType,
    DateTimePickerAdapter? adapter,
    DateTime? currentDate,
    DateTime? maxDate,
    DateTime? minDate,
    Function(DateTimeDialog dialog, DateTime date)? confirm,
    bool? showTitle = true,
    bool? useSafeArea = true,
    bool? showUnit = true,
    Widget? customSelectionOverlay,
    double? pickerHeight,
  }) : super(
            DateTimeDialogController(
              confirm: confirm,
              title: title,
              adapter: adapter,
              type: type,
              customColumnType: customColumnType,
              currentDate: currentDate,
              maxDate: maxDate,
              minDate: minDate,
              showTitle: showTitle,
              showUnit: showUnit,
              useSafeArea: useSafeArea,
              customSelectionOverlay: customSelectionOverlay,
            ),
            externalController: controller) {
    if (controller != null) {
      controller.confirm = controller.confirm ?? confirm;
      controller.title = controller.title ?? title;
      controller.adapter = controller.adapter ?? adapter;
      controller.type = controller.type ?? type;
      controller.customColumnType =
          controller.customColumnType ?? customColumnType;
      controller.currentDate = controller.currentDate ?? currentDate;
      controller.maxDate = controller.maxDate ?? maxDate;
      controller.minDate = controller.minDate ?? minDate;
      controller.showTitle = controller.showTitle ?? showTitle;
      controller.showUnit = controller.showUnit ?? showUnit;
      controller.useSafeArea = controller.useSafeArea ?? useSafeArea;
      controller.customSelectionOverlay = controller.customSelectionOverlay;
      controller.pickerHeight = controller.pickerHeight ?? 150;
    }
  }

  @override
  Widget initView(BuildContext context, DateTimeDialogController controller) {
    return Stack(
      children: [
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(12.r),
                topRight: Radius.circular(12.r),
              ),
            ),
            child: _bodySty(context, controller),
          ),
        ),
      ],
    );
  }

  _bodySty(BuildContext context, DateTimeDialogController controller) {
    if (controller.useSafeArea ?? true) {
      return SafeArea(
        top: false,
        child: _body(context, controller),
      );
    } else {
      return _body(context, controller);
    }
  }

  _body(BuildContext context, DateTimeDialogController controller) {
    return Column(
      children: [
        Visibility(
          visible: controller.showTitle ?? true,
          child: _title(controller),
        ),
        _content(controller)
      ],
    );
  }

  _title(DateTimeDialogController controller) {
    return SizedBox(
      height: 54.w,
      child: Row(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              hide();
            },
            child: Container(
              height: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              child: Text(
                "取消",
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: ColorConfig.dark_text_gray,
                  fontSize: 14.sp,
                ),
              ),
            ),
          ),
          Expanded(
            child: Text(
              controller.title ?? "",
              textAlign: TextAlign.center,
              style: TextStyle(
                color: ColorConfig.text,
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          debounceClickBtn(
            onTap: () {
              controller.confirm
                  ?.call(this, DateTime.parse(controller.adapter!.text));
            },
            child: Container(
              height: double.infinity,
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 14.w),
              child: Text(
                "确定",
                textAlign: TextAlign.center,
                style: TextStyle(
                  color: ColorConfig.FF007AFF,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  _content(DateTimeDialogController controller) {
    return Container(
      child: Picker(
          hideHeader: true,
          height: controller.pickerHeight ?? 150,
          itemExtent: 40.w,
          selectionOverlay:
              controller.customSelectionOverlay ?? _defaultSelectionOverlay(),
          textStyle: TextStyle(
              color: ColorConfig.FF020306,
              fontSize: 16.sp,
              fontWeight: FontWeight.w400),
          selectedTextStyle: TextStyle(
            color: ColorConfig.text,
            fontSize: 21.sp,
          ),
          adapter: controller.adapter!,
          title: Text(controller.title ?? ""),
          onConfirm: (Picker picker, List value) {
            logger.i((picker.adapter as DateTimePickerAdapter).value);
          }).makePicker(),
    );
  }

  _defaultSelectionOverlay() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 8.w),
      decoration: BoxDecoration(
        color: ColorConfig.black.withOpacity(0.05),
        borderRadius: BorderRadius.circular(10.r),
      ),
    );
  }
}

class DateTimeDialogController extends GetxController {
  DateTimePickerAdapter? adapter;
  String? title;
  Function(DateTimeDialog dialog, DateTime date)? confirm;
  List<int>? customColumnType;
  int? type;
  DateTime? maxDate;
  DateTime? minDate;
  DateTime? currentDate;
  bool? showTitle;
  bool? useSafeArea;
  bool? showUnit = true;
  Widget? customSelectionOverlay;
  double? pickerHeight;
  final kToday = DateTime.now();

  DateTimeDialogController(
      {this.confirm,
      this.title,
      this.adapter,
      this.type,
      this.customColumnType,
      this.currentDate,
      this.maxDate,
      this.minDate,
      this.showTitle,
      this.useSafeArea,
      this.customSelectionOverlay,
      this.pickerHeight,
      this.showUnit}) {
    adapter ??= DateTimePickerAdapter(
        type: type ?? PickerDateTimeType.kYMD,
        customColumnType: customColumnType,
        value: currentDate,
        maxValue:
            maxDate ?? DateTime(kToday.year + 10, kToday.month, kToday.day),
        minValue:
            minDate ?? DateTime(kToday.year - 10, kToday.month, kToday.day),
        isNumberMonth: true,
        yearSuffix: showUnit! ?"年" : "",
        monthSuffix: showUnit! ?"月": "",
        daySuffix: showUnit! ? "日": "",
        hourSuffix: showUnit! ? "时": "",
        minuteSuffix: showUnit! ? "分": "",
        secondSuffix: showUnit! ? "秒": "",
      );
  }
}
